<template>
    <div>
        <!-- 图片上传组件辅助-->
        <el-upload
                class="avatar-uploader"
                :action="serverUrl"
                name="file"
                :headers={Authorization:token}
                :show-file-list="false"
                :on-success="uploadSuccess"
                :on-error="uploadError"
                :before-upload="beforeUpload">
        </el-upload>
        <!--富文本编辑器组件-->
       <el-row v-loading="uillUpdateImg">
        <quill-editor
                v-model="detailContent"
                ref="myQuillEditor"
                :options="editorOption"
                @change="onEditorChange($event)"
                @ready="onEditorReady($event)"
        >
        </quill-editor>
       </el-row>
    </div>
</template>
<script>
     import { quillEditor } from 'vue-quill-editor'
    export default {
        data() {
            return {
                uillUpdateImg:false,
                token:localStorage.getItem("token"),
                quillUpdateImg: false, // 根据图片上传状态来确定是否显示loading动画，刚开始是false,不显示
                serverUrl: 'http://192.168.3.71:8013/api/pic/uploadPic',  // 这里写你要上传的图片服务器地址
                header: {token: sessionStorage.token},  // 有的图片服务器要求请求头需要有token之类的参数，写在这里
                detailContent: '', // 富文本内容
                editorOption: {
                    placeholder: '',
                    theme: 'snow',  // or 'bubble'
                    modules: {
                        toolbar: {
                            container: [
                        ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
                        ['blockquote','image'],
                        [{ 'header': 1 }, { 'header': 2 }],
                        [{ 'size': ['small', false, 'large', 'huge'] }], 
                        [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
                        [{ 'color': [] }], 
                        
                      ], // 工具栏
                            handlers: {
                                'image': function (value) {
                                    if (value) {
                                        // console.log(111)
                                        document.querySelector('.avatar-uploader input').click()
                                    } else {
                                        this.quill.format('image', false);
                                    }
                                }
                            }
                        }
                    }
                }  // 富文本编辑器配置
            }
        },
        methods: {
            // 上传图片前
            beforeUpload(res, file) {
                this.quillUpdateImg = true
            },
            // 上传图片成功
            uploadSuccess(res, file) {
                let quill = this.$refs.myQuillEditor.quill;
                if(res.code === 0){
                    let length = quill.getSelection().index;
                    quill.insertEmbed(length, 'image', res.data)
                    quill.setSelection(length + 1)
                }
                console.log(res)
            },
            // 上传图片失败
            uploadError(res, file) {
                this.quillUpdateImg = fals
            },
            onEditorReady(){},
            onEditorChange({editor, html, text}){
                
                this.detailContent = html
            }
        }
    }
</script>